<?php
class M_managedeposit extends CI_Model{
	function data_managedeposit($key){
		$data = array();
		$kuery = "SELECT t_tiket.*, users.username, users.email FROM t_tiket 
			JOIN users on users.id = t_tiket.tiket_users 
			WHERE tiket_status = 'confirm'";
		if($key !== ""){
			$kuery .= " AND LOWER(users.username) LIKE '%" . strtolower($key) . "%' OR LOWER(users.email) LIKE '%" . strtolower($key) . "%'";
		}
		$query = $this->db->query($kuery);
		$num_rows = $query->num_rows($query);
		$result = $query->result();
		$a=0;
		foreach($result as $row){
			$data[$a]=$row;
			$a++;
		}
		$result = json_encode($data);
		return $result;
	}
	function managedeposit_add($managedeposit_nama,$managedeposit_keterangan){
		$kuery = "INSERT INTO m_managedeposit(managedeposit_nama, managedeposit_keterangan) 
			VALUES ('" . $managedeposit_nama . "','" . $managedeposit_keterangan . "')";
		$query = $this->db->query($kuery);
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}
	function managedeposit_edit($managedeposit_id,$managedeposit_nama,$managedeposit_keterangan){
		$kuery = "UPDATE m_managedeposit SET managedeposit_nama = '" . $managedeposit_nama . "',managedeposit_keterangan = '" . $managedeposit_keterangan . "'
			WHERE managedeposit_id = '" . $managedeposit_id . "'";
		$query = $this->db->query($kuery);
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}
	function managedeposit_delete($managedeposit_id){
		$kuery = "DELETE FROM m_managedeposit WHERE managedeposit_id = '" . $managedeposit_id . "'";
		$query = $this->db->query($kuery);
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}
	function updatedeposit($nominal_user,$validasi_idtiket){
		$kuery = "UPDATE t_tiket SET tiket_status = 'paid' WHERE tiket_id = '" . $validasi_idtiket . "'";
		$query = $this->db->query($kuery);
		if($this->db->affected_rows()){
			$kuery_tiketuser = "SELECT tiket_users FROM t_tiket WHERE tiket_id = '" . $validasi_idtiket . "'";
			$query_tiketuser = $this->db->query($kuery_tiketuser);
			if($query_tiketuser->num_rows() > 0){
				$row = $query_tiketuser->row();
				$user_id = $row->tiket_users;
				$kuery_updatesaldo = "UPDATE m_member SET member_saldo = member_saldo + " . $nominal_user . " WHERE member_users = '" . $user_id . "'";
				$query_updatesaldo = $this->db->query($kuery_updatesaldo);
				if($this->db->affected_rows()){
					return 1;
				}else{
					return 0;
				}	
			}
		}else{
			return 0;
		}
	}
}
?>